package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.DishDisableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public void addDish(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        //载入商品数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setNumber(1);
        shoppingCart.setUserId(userId);
        //查询是否已经存在购物车中(菜品还是套餐分开考虑)
        List<ShoppingCart> list = shoppingCartMapper.select(shoppingCart);
        if(shoppingCart.getDishId() != null){
            //如果是菜品
            if(list != null && !list.isEmpty()){
                //存在则只改变number数据
                shoppingCartMapper.IncreaseNumber(list.get(0).getId());
            }
            else {
                //不存在加入商品
                Dish dish = dishMapper.selectDishById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCartMapper.save(shoppingCart);
            }
        }
        else if(shoppingCart.getSetmealId() != null){
            if(list != null && !list.isEmpty()){
                //存在则只改变number数据
                shoppingCartMapper.IncreaseNumber(list.get(0).getId());
            }
            else {
                //如果不存在则加入商品
                Setmeal setmeal = setmealMapper.selectById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCartMapper.save(shoppingCart);
            }
        }
        else {
            throw new DishDisableFailedException("选餐系统错误");
        }
    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.select(shoppingCart);
    }

    @Override
    public void clean() {
        log.info("清空用户：{} 的购物车", BaseContext.getCurrentId());
        shoppingCartMapper.deleteAll(BaseContext.getCurrentId());
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        //载入商品数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.select(shoppingCart);
        //查询是菜品还是套餐,分开考虑
        if(shoppingCartDTO.getDishId() != null){
            //如果是菜品
            if (list.get(0).getNumber() <= 1){
                //如果只有一份直接删除这个记录
                shoppingCartMapper.deleteById(list.get(0).getId());
            }
            else {
                //数量减一
                shoppingCartMapper.SubNumber(list.get(0).getId());
            }
        }
        else if(shoppingCartDTO.getSetmealId() != null){
            //如果是套餐
            if (list.get(0).getNumber() <= 1){
                //如果只有一份直接删除这个记录
                shoppingCartMapper.deleteById(list.get(0).getId());
            }
            else {
                //数量减一
                shoppingCartMapper.SubNumber(list.get(0).getId());
            }
        }
        else {
            throw new DishDisableFailedException("选餐系统错误");
        }
    }
}
